import log4js, { Configuration, Logger } from 'log4js'
import http from 'http'
import https from 'https'
import fs from 'fs'

import { Server as SocketServer } from 'socket.io'

import express from 'express'

import serveIndex from 'serve-index'



let USERCOUNT:Number = 3;

log4js.configure({
    appenders:{
        file:{
            type:'file',
            filename:'app.log',
            layout:{
                type:'pattern',
                pattern: '%r %p - %m'
            }
        }
    },
    categories:{
        default:{
            appenders:['file'],
            level:'debug'
        }
    }


} as Configuration)

let logger:Logger = log4js.getLogger();

let app = express();

app.use(serveIndex('./resources/static'))

app.use(express.static('./resources/static'))

app.all('*',(req,resp,next)=>{
    resp.header("Access-Control-Allow-Origin","*");
    resp.header("Access-Control-Allow-Headers","content-type");
    resp.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
    if(req.method.toLowerCase()=='options'){
        resp.send(200);
    }else{
        next();
    }

})

// let http_server = http.createServer(app);
// let PORT = 8080;

// http_server.listen(PORT,()=>{
//     logger.log(`Server is running on port ${PORT}`)
// });

let options={};
try
{
    options = {
        key: fs.readFileSync(__dirname+'/resources/cert.key'),
        cert: fs.readFileSync(__dirname+'/resources/cert.pem')
    }
    let https_server = https.createServer(options,app).listen(8081,()=>{
        logger.log(`Server is running on port 8081`)
    });


    let socketIo = new SocketServer(https_server,{
        
    })
}catch(e){
    console.log(e);
}






